import { drizzle } from 'drizzle-orm/mysql2'

import mysql from 'mysql2/promise'

import dotenv from 'dotenv'
import { admins } from './schema/admins'
//For env File

dotenv.config()

// 创建连接池

const connection = mysql.createPool({
  uri: process.env.DATABASE_URL,

  connectionLimit: 10,
})

// 创建 Drizzle 实例

export const db = drizzle(connection, {
  schema: {
    admins,
  },

  mode: 'default',
})

// 测试连接

export async function testConnection() {
  try {
    await connection.execute('SELECT 1')

    console.log('✅ 数据库连接成功')

    return true
  } catch (error) {
    console.error('❌ 数据库连接失败:', error)

    return false
  }
}
